package com.majsubtense.main.ui;

import com.majsubtense.core.components.ComponentType;
import com.majsubtense.core.components.UI;
import com.majsubtense.main.GameController;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.util.Duration;

public class AchievementUI extends UI {
    public static final ComponentType<AchievementUI> ANY;

    public AchievementUI() {
        super(AchievementUI.ANY);
    }

    public void showAchievementDialog(String achievementMessage) {
        pane = GameController.getCurrentGameScene().getComponent(UI.ANY).getPane();

        Label label = new Label("获得成就: " + achievementMessage);
        label.setFont(Font.font("Arial", FontWeight.BOLD, 15.0));
        label.setPrefHeight(40);
        label.setLayoutX(10);
        label.setLayoutY(-50);
        label.setTextFill(new Color(1, 1, 1, 1));
        label.setBackground(Background.fill(new Color(0.5, 0.5, 0.5, 1.0)));

        pane.getChildren().add(label);

        Timeline timeline = new Timeline();
        KeyFrame one = new KeyFrame(Duration.ZERO,
                new KeyValue(label.translateYProperty(), 0)
        );
        KeyFrame two = new KeyFrame(Duration.millis(1000),
                new KeyValue(label.translateYProperty(), 60)
        );
        KeyFrame three = new KeyFrame(Duration.millis(5000),
                new KeyValue(label.translateYProperty(), 60)
        );

        timeline.getKeyFrames().addAll(one, two, three);
        timeline.play();

        timeline.setOnFinished(event -> pane.getChildren().remove(label));
    }

    @Override
    public void attach() {
    }

    @Override
    public void update() {
    }

    @Override
    public void detach() {
    }

    static {
        ANY = new ComponentType<>(UI.ANY, "ACHIEVEMENT");
    }
}
